package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.dto.UmsMenuDto;
import com.genesis.mall.entity.UmsMenu;
import com.genesis.mall.entity.UmsRoleMenuRelation;
import com.genesis.mall.service.IUmsMenuService;
import com.genesis.mall.service.IUmsRoleMenuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/menu")
public class UmsMenuController {
    @Autowired
    private IUmsMenuService umsMenuService;

    @Autowired
    private IUmsRoleMenuRelationService roleMenuRelationService;
    /**
     * 树形结构返回所有菜单列表
     */
    @GetMapping("/treeList")
    public Result<List<UmsMenuDto>> treeList(){
        List<UmsMenuDto> treeList = umsMenuService.getTreeList();
        return Result.success(treeList);
    }

    /**
     * 菜单分页查询
     * @param parentId
     * @param pageDto
     * @return
     */
    @GetMapping("/list/{parentId}")
    public Result<PageDto<UmsMenu>> list(@PathVariable Long parentId, PageDto<UmsMenu> pageDto){
        //分页查询
        Page<UmsMenu> page = new Page<>(pageDto.getPageNum(), pageDto.getPageSize());
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,parentId);
        umsMenuService.page(page,lqw);
        //封装返回数据
        pageDto.setTotal((int)page.getTotal());
        pageDto.setTotalPage((int)page.getPages());
        pageDto.setList(page.getRecords());
        return Result.success(pageDto);
    }

    /**
     * 添加菜单
     * @param umsMenu
     * @return
     */
    @PostMapping("/create")
    public Result<String> create(@RequestBody UmsMenu umsMenu){
        if (umsMenu.getParentId()==0){
            umsMenu.setLevel(0);
        }else {
            umsMenu.setLevel(1);
        }
        //设置创建时间
        umsMenu.setCreateTime(LocalDateTime.now());
        //将数据添加到数据库
        umsMenuService.save(umsMenu);
        return Result.success();
    }

    /**
     * 根据id删除后台菜单
     */
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        LambdaQueryWrapper<UmsRoleMenuRelation> lqw =new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleMenuRelation::getMenuId,id);
        List<UmsRoleMenuRelation> menuRelations = roleMenuRelationService.list(lqw);
        if (menuRelations!=null){
            return Result.error("有关联角色,无法删除");
        }
        umsMenuService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UmsMenu> findById(@PathVariable Long id){
        UmsMenu umsMenu = umsMenuService.getById(id);
        return Result.success(umsMenu);
    }

    /**
     * 修改菜单数据
     */
    @PostMapping("/update/{id}")
    public Result<String> update(@RequestBody UmsMenu umsMenu){
        umsMenuService.updateById(umsMenu);
        return Result.success();
    }

    /**
     * 修改菜单状态
     * @param id
     * @param hidden
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public Result<String> updateHidden(@PathVariable Long id,Integer hidden){
        //根据id查询菜单数据
        UmsMenu umsMenu = umsMenuService.getById(id);
        //修改状态
        umsMenu.setHidden(hidden);
        //修改数据
        umsMenuService.updateById(umsMenu);
        return Result.success();
    }
}
